package com.tzq.autoticket;

import com.tzq.autoticket.bean.Tourist;
import com.tzq.autoticket.utils.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] a){
        try {
            ProcessBuilder pb=new ProcessBuilder("tesseract.exe C:\\Users\\Administrator\\Desktop\\createimage.jpg - -psm 8 -l chi_sim".split(" "));
//            ProcessBuilder pb=new ProcessBuilder("java","-version");
//            ProcessBuilder pb=new ProcessBuilder("dir");
//            Map<String, String> env = pb.environment();
//            env.put("VAR1", "myValue");
//            env.remove("OTHERVAR");
//            env.put("VAR2", env.get("VAR1") + "suffix");
            pb.directory(new File("D:\\Softwares\\Tesseract-OCR"));
            Process p = pb.start();
            getCmdLines(p);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<String> getCmdLines(Process p) {
        List<String> lines = new ArrayList<>();
        try {
            String inputLine, errorLine;
            BufferedReader inputBr = null;
            BufferedReader errorBr = null;
            try {
                inputBr = new BufferedReader(new InputStreamReader(p.getInputStream()),
                        1024);
                errorBr = new BufferedReader(new InputStreamReader(p.getErrorStream()),
                        1024);
                while ((inputLine = inputBr.readLine()) != null) {
                    Log.info(inputLine);
                    lines.add(inputLine);
                }
                while ((errorLine = errorBr.readLine()) != null) {
                    Log.err(errorLine);
                    lines.add(errorLine);
                }
            } catch (IOException e) {
                e.printStackTrace();
                Log.err(e.getMessage());
            } finally {
                if(inputBr!=null){
                    inputBr.close();

                }
                if(errorBr!=null){
                    errorBr.close();
                }
                if (p != null) {
                    p.destroy();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }


    public static List<Tourist> getTestList() {
        List<Tourist> list = new ArrayList<>();
//        Tourist tourist = new Tourist();
//        tourist.setName("唐郑钤");
//        tourist.setCardType("身份证");
//        tourist.setCardNo("513029199103180451");
//        tourist.setPhoneNumber("15987654231");
//        list.add(tourist);
//
//        Tourist tourist1 = new Tourist();
//        tourist1.setName("氛围");
//        tourist1.setCardType("身份证");
//        tourist1.setCardNo("110101200001012999");
//        tourist1.setPhoneNumber("15987632231");
//        list.add(tourist1);

        Tourist tourist2 = new Tourist();
        tourist2.setName("张师2");
        tourist2.setCardType("身份证");
        tourist2.setCardNo("14020320000101519x");
        tourist2.setPhoneNumber("15993423231");
        list.add(tourist2);
        return list;
    }
}
